import {createStore } from 'vuex'
import {Menu,Ch} from '@/Json/menu.ts'
import {
    getStore,
    setStore,
    clearStore
  } from '@/utils/store'
interface IAdmin  {
    password:number
    userName:string
    title:string
}

interface TokenModel {
    tokenKey:string
}
interface  MenuModel{
    menuType: string
    // data: Menu|Ch
}
export interface  showMenuModel{
    // menuType: string
    data: Menu|Ch
}
interface headerActiveModel{
    headerActive:object
}
export interface IState{
    userInfo: IAdmin
    tokenKey: TokenModel
    menuType: MenuModel
    showMenuArr: showMenuModel
    headerActive: headerActiveModel
}
const state: IState = {
    userInfo: {  } as IAdmin,
    tokenKey: '' as unknown as TokenModel,
    menuType: '' as unknown as  MenuModel,
    showMenuArr: [] as unknown as showMenuModel,
    headerActive:  { } as  headerActiveModel
}

const getters = {
    getUserInfo(state?:IState){
        return getStore({ name: 'userInfo'})||state?.userInfo
    },
    getToken(state?:IState){
        return getStore({ name: 'token'})||state?.tokenKey
    },
    getMenuType(state?:IState){
        return getStore({ name: 'menuType'})||state?.menuType
    },
    getMenu(state:IState) {
        state.showMenuArr = getStore({ name: 'showMenuArr'})||[]
        return state?.showMenuArr;
    },
    getHeaderActive(state:IState){
        state.headerActive = getStore({ name: 'headerActive'})||{ key: '1'}
        return state?.headerActive;
    }
}

const  mutations ={
    setUserInfo(state:IState,admin:IAdmin){
        state.userInfo = admin
        setStore({
            name: 'userInfo',
            content: admin,
            type: 'session'
        })
    },
    setToken(state:IState,token:TokenModel){
        state.tokenKey = token
        setStore({
            name: 'token',
            content: token,
            type: 'session'
        })
    },
    setMenuType(state:IState,type:any){
        const {menuType ,data} = type
        state.menuType = type.menuType
        if(menuType === 'double'){
            state.showMenuArr = data
        }else if(menuType === 'single'){
            state.showMenuArr = data
        }
        setStore({
            name: 'menuType',
            content: menuType,
            type: 'session'
        })
        setStore({
            name: 'showMenuArr',
            content: state.showMenuArr,
            type: 'session'
        })
    },
    setHeaderActive(state:IState,type:headerActiveModel){
        state.headerActive = type
        setStore({
            name: 'headerActive',
            content: state.headerActive,
            type: 'session'
        })
    },
    setClearStore(state:IState){
        clearStore({
            type:'sessionStorage'
        })
    }
}

const store = createStore({
    state:state,
    getters:getters,
    mutations:mutations
})

export default store
